﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EZCRS.HCMS.Bll.baseinfo;
using EZCRS.HCMS.Entity;
using System.Collections.Generic;

namespace EZCRS.HCMS.Web.manage.ta
{
    public partial class ta_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                int iChannelID = 0;

                PageInit();

                if (!string.IsNullOrEmpty(Request.QueryString["channel"]))
                {
                    int.TryParse(Request.QueryString["channel"], out iChannelID);

                    ddlChannel.SelectedValue = iChannelID.ToString();
                }

                if (Request.QueryString["action"] != null)
                {
                    if (Request.QueryString["action"].ToLower() == "get")
                    {
                        //get data
                        int iPageIndex = 1;
                        int iPageSize = 15;

                        if (!string.IsNullOrEmpty(Request.Params["page"]))
                            int.TryParse(Request.Params["page"], out iPageIndex);

                        if (!string.IsNullOrEmpty(Request.Params["rp"]))
                            int.TryParse(Request.Params["rp"], out iPageSize);

                        Response.Write(GetJSON(iPageIndex, iPageSize, Request.Params["query"], iChannelID));
                        Response.End();
                    }
                    else if (Request.QueryString["action"].ToLower() == "del")
                    {
                        //删除
                        if (!string.IsNullOrEmpty(Request.Params["id"]))
                        {
                            bool b = DeleteIATA(Request.Params["id"]);

                            if (b)
                                Response.Write("{state:1}");
                            else
                                Response.Write("{state:0}");

                            Response.End();
                        }
                    }
                }
            }
        }

        private void PageInit()
        {

            EZCRS.HCMS.Bll.AIR.TAChannel channel = new EZCRS.HCMS.Bll.AIR.TAChannel();

            List<EZCRS.HCMS.Entity.Channel> lstChannel = channel.GetAll();

            foreach (EZCRS.HCMS.Entity.Channel c in lstChannel)
            {
                ListItem li = new ListItem();

                li.Text = c.Name;
                li.Value = c.ID.ToString();

                ddlChannel.Items.Add(li);
            }

        }

        private bool DeleteIATA(string IATA)
        {
            HCRS_TA_BLL bllPlan = new HCRS_TA_BLL();

            bllPlan.DeleteTA(IATA);

            return true;
        }


        private void ParseQuery(string Query, out int ChannelID, out string Name)
        {
            ChannelID = 0;

            Name = null;

            string[] strQy = Query.Split(',');

            if (strQy.Length >= 2)
            {
                int.TryParse(strQy[1], out ChannelID);

                Name = strQy[0];
            }
        }

        /// <summary>
        /// Get JSON
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Query"></param>
        /// <returns></returns>
        private string GetJSON(int PageIndex, int PageSize, string Query, int ChannelID)
        {

            string strName = string.Empty;

            if(!string.IsNullOrEmpty(Query))
                ParseQuery(Query, out ChannelID, out strName);

            HCRS_TA_BLL bllPlan = new HCRS_TA_BLL();

            int iRowCount = 0;

            //int iLan = base.LanguageId;

            List<HCRS_TA> lstTA = bllPlan.Search(ChannelID, strName, PageIndex, PageSize, out iRowCount);

            string strJson = "{\"total\":" + iRowCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

            foreach (HCRS_TA ta in lstTA)
            {
                strJson += "{\"id\":\"" + ta.IATA + "\",";
                strJson += "\"cell\":[";

                strJson += "\"" + HttpUtility.HtmlEncode(ta.IATA) + "\",";

                if (ta.Name.Length > 80)
                    ta.Name = ta.Name.Substring(0, 80) + "...";

                strJson += "\"" + HttpUtility.HtmlEncode(ta.Name) + "\",";
                strJson += "\"" + HttpUtility.HtmlEncode(ta.Linkman) + "\",";
                strJson += "\"" + HttpUtility.HtmlEncode(ta.Tel) + "\",";
                strJson += "\"" + HttpUtility.HtmlEncode(ta.Fax) + "\",";

                strJson += "\"" + ta.CrateDate.ToString("yyyy-MM-dd") + "\"";

                strJson += "]},";
            }

            strJson = strJson.Replace("\r\n", " ");

            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }
    }

}
